package com.sunnada.edu.business.worker.service;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.pub.cache.SysParam;
import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.common.MD5;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.http.HttpRequest;
import com.sunnada.edu.system.pub.util.Utils;

public class WorkerMethods {

	public static int restPwd(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String userId = params.get("userId");
		String password = params.get("password");
		password = MD5.md5(userId + password);
		String sql = "update S_SYSUSER set  LOGIN_PWD='" + password
				+ "' where USER_ID='" + userId + "'";
		int num = dao.update(sql);
		return num;

	}

	public static int getWorkerCotByJobNo(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String jobno = params.get("jobno");
		String sql = "SELECT REC_ID FROM T_WORKER T WHERE  JOBNO='" + jobno
				+ "'AND REC_STATUS!='2' ";
		return dao.selectListCot(sql);
	}

	/**
	 * 工人配置合同
	 * @param params
	 * @return
	 */
	public static int contractAdd(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String owner = params.get("recId");
		String contract = params.get("fileName");
		String contractPath = params.get("path");
		String recId = UUID.randomUUID().toString();
		String sql = "insert into  T_CONTRACT(rec_id,name,type,type2,owner,path,rec_status)  value('"
				+ recId
				+ "','"
				+ contract
				+ "','2','1','"
				+ owner
				+ "','"
				+ contractPath + "','0')";
		int num = dao.insert(sql);
		return num;

	}
	
	public static List getContractList(HashMap<String, String> params){	
		PubDao dao = AppContext.getInstance().getPubDao();
		String recId=params.get("recId");
		String sql=" select rec_id,name,path from t_contract where rec_status!='2' and owner='"+recId+"'";
		return dao.selectList(sql);	
	}
	
	public static int delContractById(HashMap<String, String> params){	
		PubDao dao = AppContext.getInstance().getPubDao();
		String recId=params.get("recId");
		String sql=" update t_contract set rec_status='2' where rec_id='"+recId+"'";
		return dao.update(sql);	
	}

	public static int contractDisable(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String recId=params.get("recId");
		String sql=" update t_worker set htstate='2',htedtime=now() where rec_id='"+recId+"'";
		return dao.update(sql);	
	}

	public static int getWorkerCotByTel(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String tel = params.get("tel");
		String sql = "select rec_id from t_worker t where  tel='" + tel
				+ "'and rec_status!='2' ";
		return dao.selectListCot(sql);
	}
	
	
	/**
	 * 向外部系统发送新增工人
	 * @param params
	 * @return
	 */
	public static int sendAddWorkInfo(HashMap<String, String> params) {
		String jobno = params.get("jobno");
		String name = params.get("name");
		String groupid = params.get("groupid");
		String ctp=params.get("ctp");
		String photo =ctp+"/"+params.get("photo");
		String URL=SysParam.WORKER_URL_ADD;
		int result =0;//失败
		try {
			MultipartEntity reqEntity = new MultipartEntity();
			reqEntity.addPart("userId", new StringBody(jobno));
			reqEntity.addPart("userName", new StringBody(name));
			//reqEntity.addPart("agenciesid", new StringBody(groupid));
			reqEntity.addPart("index", new StringBody("1"));
			reqEntity.addPart("avatar", new FileBody(new File(photo)));
			JSONObject resp = HttpRequest.httpPostSendMultipartEntity(URL,reqEntity);
			result = resp.getIntValue("status");//接口是否调用成功
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 向外部系统发送修改工人
	 * @param params
	 * @return
	 */
	public static int sendUpdateWorkInfo(HashMap<String, String> params) {
		String jobno = params.get("jobno");
		String name = params.get("name");
		String groupid = params.get("groupid");
		String ctp=params.get("ctp");
		String photo =params.get("photo");
		if(Utils.isNull(photo)){
			photo=params.get("photoTemp");
		}
		photo=ctp+"/"+photo;
		String URL=SysParam.WORKER_URL_UPDATE;
		int result =0;//失败
		try {
			MultipartEntity reqEntity = new MultipartEntity();
			reqEntity.addPart("userId", new StringBody(jobno));
			reqEntity.addPart("userName", new StringBody(name));
			reqEntity.addPart("agenciesid", new StringBody(groupid));
			reqEntity.addPart("index", new StringBody("1"));
			reqEntity.addPart("face", new FileBody(new File(photo)));
			JSONObject resp = HttpRequest.httpPostSendMultipartEntity(URL,reqEntity);
			result = resp.getIntValue("status");//接口是否调用成功
			String message = resp.getString("message");//返回描述
			String data = resp.getString("data");//接口返回内容，值可能为对象、数组等任何业务方约定好的类型
			String code = resp.getString("code");//错误编码
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 向外部系统发送删除工人
	 * @param params
	 * @return
	 */
	public static int sendDelWorkInfo(String jobno) {
		String URL=SysParam.WORKER_URL_DEL;
		int result =0;//失败
		try {
			MultipartEntity reqEntity = new MultipartEntity();
			reqEntity.addPart("userId", new StringBody(jobno));
			JSONObject resp =HttpRequest.httpPostSendMultipartEntity(URL,reqEntity);
			result = resp.getIntValue("status");//接口是否调用
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public static void setWorkerType(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerTypeDiy = params.get("workerTypeDiy");
		if(Utils.isNotNull(workerTypeDiy)){
			//查看类型名是否存在
			String sql=" select rec_id from s_dict where  rec_value='"+workerTypeDiy+"' and rec_type='WORKER_TP'";
			List list=dao.selectList(sql);
			if(list.size()==0){
				String recId=UUID.randomUUID().toString();
				String recId2=UUID.randomUUID().toString();
				sql = "insert into s_dict(rec_id,rec_key,rec_value,rec_type)values('"+recId+"','"+recId2+"','"+workerTypeDiy+"','WORKER_TP')";
				dao.insert(sql);
			}
		}
	}
	
	
	public static void delGroupMonitor(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String recIds = params.get("recIds");
		String sql = "update t_worker_group set monitor='' ,monitor_id='',tel='' where monitor_id in("+recIds+")";
		dao.selectListCot(sql);
		
	}

	public static void updateMontor(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String recId = params.get("recId");
		String name = params.get("name");
		String tel = params.get("tel");
		String sql = "update t_worker_group set monitor='"+name+"',TEL='"+tel+"'  where monitor_id ='"+recId+"'";
		dao.selectListCot(sql);
	}

}
